#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   DTC_test.py
@Time    :   2023/05/22 13:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   YangLiang@chengtech.com
@License :   (C)Copyright 2023-2026
@Desc    :   None
'''

# here put the import lib
from pytest_assume.plugin import assume
 
import pytest
import binascii
import logging
import time
import Set_up
from commDriver.modbus_plc import *
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True


# @allure.severity('critical')
# class Test_Electrical_inspection(object):
#    '''
#    软件在每次发布版本前进行一定cycle的测试:
#    需要按照整车实际总线负载进行模拟,第一次测试相关功能修改1000cycle,常规500cycle
#    '''

#    def Comms_Setup(self, Driver, iStep):
#       Driver['CAN'].set_signal_IGN(2)
#       Driver['CAN'].set_signal_Speed(0)
#       with allure.step(f'Step{iStep}: Request:0x10,0x03, Rsponse:0x50,0x03'):
#          uds_rep,size = Driver['UDS'].req_and_res_can([0x10,0x03],2)
#          if  uds_rep[0:2] != [0x50,0x03]:
#             actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
#             logging.error(f'correct response:[0x50,0x03,0x00,0x32,0x00,0xC8], actual response:[{actuRespStr}]')
#          with assume: assert uds_rep[0:2] == [0x50,0x03]
#          iStep += 1
#       with allure.step(f"Step{iStep}: Request:0x27,0x01, # Response:0x67,0x01"):
#          seed = []
#          uds_rep,size = Driver['UDS'].req_and_res_can([0x27,0x01],2)
#          if  uds_rep[0:2] != [0x67,0x01]:
#             actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')  
#             logging.error(f'correct response:[0x67,0x01], actual response:[{actuRespStr}]')
#          with assume: assert (uds_rep[0:2] == [0x67,0x01] and size == 6)
#          iStep += 1
   
#          seed = uds_rep[2:6]
#          logging.info(f'seed:{seed}')
      
#          key = Driver['CAN'].calcKey(seed)
#          send_key = [0x27,0x02] + key
#       with allure.step(f"Step{iStep}: Request:{send_key}, # Response:0x67,0x02"):
#          uds_rep,size = Driver['UDS'].req_and_res_can(send_key,len(send_key))
#          if  uds_rep[0:2] != [0x67,0x02]:
#             actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')  
#             correct_send_key = binascii.b2a_hex(bytearray(send_key), ' ')  
#             logging.error(f'correct response:[{correct_send_key}], actual response:[{actuRespStr}]')
#          with assume: assert uds_rep[0:2] == [0x67,0x02]
#          iStep += 1
#       return iStep

#    def ClearDTCs(self, Driver, iStep):

#       with allure.step(f"Step{iStep}: Request:0x14,0xFF,0xFF,0xFF, Rsponse:0x54"):
#          uds_rep,size = Driver['UDS'].req_and_res_can([0x14,0xFF,0xFF,0xFF],4)
#          if  uds_rep[0] != 0x54:
#             actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
#             logging.error(f'correct response:[0x54], actual response:[{actuRespStr}]')
#          with assume: assert uds_rep[0] == 0x54
#          time.sleep(0.01)
#          iStep += 1
#       return iStep

#    def Read_Version_Information(self, Driver, iStep):

#       with allure.step(f"Step{iStep}: Request:0x22,0xF1,0x87, Rsponse:0x62,0xF1,0x87"):
#          uds_rep,size = Driver['UDS'].req_and_res_can([0x22,0xF1,0x87],3)
#          if  uds_rep[0:12] != [0x62,0xF1,0x87]+ Set_up.PartNumber:
#             actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
#             logging.error(f'correct response:[0x62,0xF1,0x87]+ [{Set_up.PartNumber}], actual response:[{actuRespStr}]')
#          with assume: assert uds_rep[0:12] == [0x62,0xF1,0x87]+ Set_up.PartNumber
#          iStep += 1

#       with allure.step(f"Step{iStep}: Request:0x22,0xF1,0x8A, Rsponse:0x62,0xF1,0x8A"):
#          uds_rep,size = Driver['UDS'].req_and_res_can([0x22,0xF1,0x8A],3)
#          if  uds_rep[0:13] != [0x62,0xF1,0x8A]+ Set_up.SupplierCode:
#             actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
#             logging.error(f'correct response:[0x62,0xF1,0x8A]+ [{Set_up.SupplierCode}], actual response:[{actuRespStr}]')
#          with assume: assert uds_rep[0:13] == [0x62,0xF1,0x8A]+ Set_up.SupplierCode
#          iStep += 1

#       with allure.step(f"Step{iStep}: Request:0x22,0xF1,0x91, Rsponse:0x62,0xF1,0x91"):
#          uds_rep,size = Driver['UDS'].req_and_res_can([0x22,0xF1,0x91],3)
#          if  uds_rep[0:12] != [0x62,0xF1,0x91]+ Set_up.HardwareVersionNumber:
#             actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
#             logging.error(f'correct response:[0x62,0xF1,0x91]+ [{Set_up.HardwareVersionNumber}], actual response:[{actuRespStr}]')
#          with assume: assert uds_rep[0:12] == [0x62,0xF1,0x91]+ Set_up.HardwareVersionNumber
#          iStep += 1

#       with allure.step(f"Step{iStep}: Request:0x22,0xF1,0x95, Rsponse:0x62,0xF1,0x95"):
#          uds_rep,size = Driver['UDS'].req_and_res_can([0x22,0xF1,0x95],3)
#          if  uds_rep[0:12] != [0x62,0xF1,0x95]+ Set_up.ECUSoftwareversionnumber:
#             actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
#             logging.error(f'correct response:[0x62,0xF1,0x95]+ [{Set_up.ECUSoftwareversionnumber}], actual response:[{actuRespStr}]')
#          with assume: assert uds_rep[0:12] == [0x62,0xF1,0x95] + Set_up.ECUSoftwareversionnumber
#          iStep += 1
#       return iStep

#    def Write_Key_information_of_vehicle(self, Driver, iStep):

#       with allure.step(f"Step{iStep}: Request:0x2E,0xF1,0x90, Rsponse:0x6E,0xF1,0x90"):
#          uds_rep,size = Driver['UDS'].req_and_res_can([0x2E,0xF1,0x90,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01],20)
#          if  uds_rep[0:3] != [0x6E,0xF1,0x90] :
#             actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
#             logging.error(f'correct response:[0x62,0xF1,0x90], actual response:[{actuRespStr}]')
#          with assume: assert uds_rep[0:3] == [0x6E,0xF1,0x90] 
#          iStep += 1
#       return iStep

#    def Reset_ECU(self, Driver, iStep):
#       time.sleep(1)

#       with allure.step(f"Step{iStep}: Request:0x11,0x01, Rsponse:0x51,0x01"):
#          uds_rep,size = Driver['UDS'].req_and_res_can([0x11,0x01],2)
#          if  uds_rep[0:2] != [0x51,0x01]:
#             actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
#             logging.error(f'correct response:[0x51,0x01], actual response:[{actuRespStr}]')
#          with assume: assert uds_rep[0:2] == [0x51,0x01]
#          time.sleep(2)
#          iStep += 1
#       return iStep

#    def Read_VIN(self, Driver, iStep):

#       with allure.step(f"Step{iStep}: Request:0x22,0xF1,0x90, Rsponse:0x62,0xF1,0x90"):
#          uds_rep,size = Driver['UDS'].req_and_res_can([0x22,0xF1,0x90],3)
#          with assume: assert uds_rep[0:20] == [0x62,0xF1,0x90,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01] 
#          iStep += 1
#       return iStep

#    def Read_DTCs(self, Driver, iStep):
#       with allure.step(f"Step{iStep}: Request:0x19,0x02,0x09, Rsponse:0x59,0x02,0x09"):
#          uds_rep,size = Driver['UDS'].req_and_res_can([0x19,0x02,0x09],3)
#          if  uds_rep[0:3] != [0x59,0x02,0x09]:
#             actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
#             logging.error(f'correct response:[0x59,0x02,0x09], actual response:[{actuRespStr}]')
#          with assume: assert uds_rep[0:3] == [0x59,0x02,0x09] 
#          iStep += 1

#       with allure.step(f"Step{iStep}: Request:0x19,0x01,0x09, Rsponse:0x59,0x02,0x09"):
#          uds_rep,size = Driver['UDS'].req_and_res_can([0x19,0x01,0x09],3)
#          if  uds_rep[0:6] != [0x59,0x01,0x09,0x00,0x00,0x00]:
#             actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
#             logging.error(f'correct response:[0x59,0x02,0x09], actual response:[{actuRespStr}]')
#          with assume: assert uds_rep[0:6] == [0x59,0x01,0x09,0x00,0x00,0x00] 
#          iStep += 1
#       return iStep

#    @pytest.mark.repeat(100)
#    def test_electrical_inspection(self, Driver):   # 回放过程中 总线负载大概在55%~60%
      
#       iStep = 1
#       iStep = self.Comms_Setup(Driver, iStep)
#       iStep = self.ClearDTCs(Driver, iStep)
#       iStep = self.Read_Version_Information(Driver, iStep)
#       iStep = self.Write_Key_information_of_vehicle(Driver, iStep)
#       iStep = self.Reset_ECU(Driver, iStep)
#       iStep = self.ClearDTCs(Driver, iStep)
#       iStep = self.Read_VIN(Driver, iStep)
#       iStep = self.Read_DTCs(Driver, iStep)

@allure.severity('critical')
class Test_Electrical_inspection_for_CAR(object):
   '''
   软件在每次发布版本前进行一定cycle的测试:
   需要按照整车实际总线负载进行模拟,第一次测试相关功能修改1000cycle,常规500cycle
   '''

   def Comms_Setup(self, Driver, dirve, iStep):
      Driver['CAN'].set_signal_IGN(2)
      Driver['CAN'].set_signal_Speed(0)
      with allure.step(f'Step{iStep}: Request:0x10,0x03, Rsponse:0x50,0x03'):
         uds_rep,size = dirve.req_and_res_can([0x10,0x03],2)
         if  uds_rep[0:2] != [0x50,0x03]:
            actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
            logging.error(f'correct response:[0x50,0x03,0x00,0x32,0x00,0xC8], actual response:[{actuRespStr}]')
         with assume: assert uds_rep[0:2] == [0x50,0x03]
         iStep += 1
      with allure.step(f"Step{iStep}: Request:0x27,0x01, # Response:0x67,0x01"):
         seed = []
         uds_rep,size = dirve.req_and_res_can([0x27,0x01],2)
         if  uds_rep[0:2] != [0x67,0x01]:
            actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')  
            logging.error(f'correct response:[0x67,0x01], actual response:[{actuRespStr}]')
         with assume: assert (uds_rep[0:2] == [0x67,0x01] and size == 18)
         iStep += 1
   
         seed = uds_rep[2:]
         logging.info(f'seed:{seed}')
      
         key = Driver['CAN'].calcKey(seed)
         send_key = [0x27,0x02] + key
      with allure.step(f"Step{iStep}: Request:{send_key}, # Response:0x67,0x02"):
         uds_rep,size = dirve.req_and_res_can(send_key,len(send_key))
         if  uds_rep[0:2] != [0x67,0x02]:
            actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ')  
            correct_send_key = binascii.b2a_hex(bytearray(send_key), ' ')  
            logging.error(f'correct response:[{correct_send_key}], actual response:[{actuRespStr}]')
         with assume: assert uds_rep[0:2] == [0x67,0x02]
         iStep += 1
      return iStep

   def ClearDTCs(self, dirve, iStep):

      with allure.step(f"Step{iStep}: Request:0x14,0xFF,0xFF,0xFF, Rsponse:0x54"):
         uds_rep,size = dirve.req_and_res_can([0x14,0xFF,0xFF,0xFF],4)
         if  uds_rep[0] != 0x54:
            actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
            logging.error(f'correct response:[0x54], actual response:[{actuRespStr}]')
         with assume: assert uds_rep[0] == 0x54
         time.sleep(0.01)
         iStep += 1
      return iStep

   def Read_Version_Information(self, dirve, iStep):

      with allure.step(f"Step{iStep}: Request:0x22,0xF1,0x87, Rsponse:0x62,0xF1,0x87"):
         uds_rep,size = dirve.req_and_res_can([0x22,0xF1,0x87],3)
         if  uds_rep[0:12] != [0x62,0xF1,0x87]+ Set_up.PartNumber:
            actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
            logging.error(f'correct response:[0x62,0xF1,0x87]+ [{Set_up.PartNumber}], actual response:[{actuRespStr}]')
         with assume: assert uds_rep[0:12] == [0x62,0xF1,0x87]+ Set_up.PartNumber
         iStep += 1

      with allure.step(f"Step{iStep}: Request:0x22,0xF1,0x8A, Rsponse:0x62,0xF1,0x8A"):
         uds_rep,size = dirve.req_and_res_can([0x22,0xF1,0x8A],3)
         if  uds_rep[0:13] != [0x62,0xF1,0x8A]+ Set_up.SupplierCode:
            actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
            logging.error(f'correct response:[0x62,0xF1,0x8A]+ [{Set_up.SupplierCode}], actual response:[{actuRespStr}]')
         with assume: assert uds_rep[0:13] == [0x62,0xF1,0x8A]+ Set_up.SupplierCode
         iStep += 1

      with allure.step(f"Step{iStep}: Request:0x22,0xF1,0x91, Rsponse:0x62,0xF1,0x91"):
         uds_rep,size = dirve.req_and_res_can([0x22,0xF1,0x91],3)
         if  uds_rep[0:12] != [0x62,0xF1,0x91]+ Set_up.HardwareVersionNumber:
            actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
            logging.error(f'correct response:[0x62,0xF1,0x91]+ [{Set_up.HardwareVersionNumber}], actual response:[{actuRespStr}]')
         with assume: assert uds_rep[0:12] == [0x62,0xF1,0x91]+ Set_up.HardwareVersionNumber
         iStep += 1

      with allure.step(f"Step{iStep}: Request:0x22,0xF1,0x95, Rsponse:0x62,0xF1,0x95"):
         uds_rep,size = dirve.req_and_res_can([0x22,0xF1,0x95],3)
         if  uds_rep[0:12] != [0x62,0xF1,0x95]+ Set_up.ECUSoftwareversionnumber:
            actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
            logging.error(f'correct response:[0x62,0xF1,0x95]+ [{Set_up.ECUSoftwareversionnumber}], actual response:[{actuRespStr}]')
         with assume: assert uds_rep[0:12] == [0x62,0xF1,0x95] + Set_up.ECUSoftwareversionnumber
         iStep += 1
      return iStep

   def Write_Key_information_of_vehicle(self, dirve, iStep):

      with allure.step(f"Step{iStep}: Request:0x2E,0xF1,0x90, Rsponse:0x6E,0xF1,0x90"):
         uds_rep,size = dirve.req_and_res_can([0x2E,0xF1,0x90,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01],20)
         if  uds_rep[0:3] != [0x6E,0xF1,0x90] :
            actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
            logging.error(f'correct response:[0x62,0xF1,0x90], actual response:[{actuRespStr}]')
         with assume: assert uds_rep[0:3] == [0x6E,0xF1,0x90] 
         iStep += 1
      return iStep

   def Reset_ECU(self, dirve, iStep):
      time.sleep(1)

      with allure.step(f"Step{iStep}: Request:0x11,0x01, Rsponse:0x51,0x01"):
         uds_rep,size = dirve.req_and_res_can([0x11,0x01],2)
         if  uds_rep[0:2] != [0x51,0x01]:
            actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
            logging.error(f'correct response:[0x51,0x01], actual response:[{actuRespStr}]')
         with assume: assert uds_rep[0:2] == [0x51,0x01]
         time.sleep(2)
         iStep += 1
      return iStep

   def Read_VIN(self, dirve, iStep):

      with allure.step(f"Step{iStep}: Request:0x22,0xF1,0x90, Rsponse:0x62,0xF1,0x90"):
         uds_rep,size = dirve.req_and_res_can([0x22,0xF1,0x90],3)
         with assume: assert uds_rep[0:20] == [0x62,0xF1,0x90,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01] 
         iStep += 1
      return iStep

   def Read_DTCs(self, dirve, iStep):
      with allure.step(f"Step{iStep}: Request:0x19,0x02,0x09, Rsponse:0x59,0x02,0x09"):
         uds_rep,size = dirve.req_and_res_can([0x19,0x02,0x09],3)
         if  uds_rep[0:3] != [0x59,0x02,0x09]:
            actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
            logging.error(f'correct response:[0x59,0x02,0x09], actual response:[{actuRespStr}]')
         with assume: assert uds_rep[0:3] == [0x59,0x02,0x09] 
         # a,b,c = dirve.search_DTC([0x19,0x02,0x0B],[0xF2,0x4C,0x88,0x08])
         # assert a == False
         
         
         iStep += 1

      with allure.step(f"Step{iStep}: Request:0x19,0x01,0x09, Rsponse:0x59,0x02,0x09"):
         uds_rep,size = dirve.req_and_res_can([0x19,0x02,0x0B],3)
         uds_rep,size = dirve.req_and_res_can([0x19,0x01,0x09],3)
         # if  uds_rep[0:6] != [0x59,0x01,0x09,0x00,0x00,0x00]:
         #    actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
         #    logging.error(f'correct response:[0x59,0x01,0x09], actual response:[{actuRespStr}]')
         # with assume: assert uds_rep[0:6] == [0x59,0x01,0x09,0x00,0x00,0x00] 
         iStep += 1
      return iStep

   @pytest.mark.repeat(100)
   def test_electrical_inspection_on_car(self, Driver):   # 回放过程中 总线负载大概在55%~60%
      driver_UDS = [Driver['UDSRRCR'],Driver['UDSLRCR']]
      for dirve in driver_UDS:
         iStep = 1
         uds_rep,size = Driver['UDSRRCR'].req_and_res_can([0x14,0xFF,0xFF,0xFF],4)
         iStep = self.Comms_Setup(Driver, dirve, iStep)
         iStep = self.ClearDTCs(dirve, iStep)
         iStep = self.Read_Version_Information(dirve, iStep)
         iStep = self.Write_Key_information_of_vehicle(dirve, iStep)
         iStep = self.Reset_ECU(dirve, iStep)
         # iStep = self.ClearDTCs(dirve, iStep)
         iStep = self.Read_VIN(dirve, iStep)
         time.sleep(10)
         a,b,c = Driver['UDSLRCR'].search_DTC([0x19,0x02,0x0B],[0x98,0x7C,0x54,0x08],2)
         with assume:assert a == False
         a,b,c = Driver['UDSRRCR'].search_DTC([0x19,0x02,0x0B],[0x98,0x7C,0x54,0x08],1)
         with assume:assert a == False
         iStep = self.Read_DTCs(dirve, iStep)
         uds_rep,size = Driver['UDSLRCR'].req_and_res_can([0x19,0x02,0x0B],3)
         uds_rep,size = Driver['UDSRRCR'].req_and_res_can([0x19,0x02,0x0B],3)
         
      
    


   